import 'package:flustars/flustars.dart';
import 'package:flutter_getx/app/config/constant.dart';
import 'package:flutter_getx/core/ext_core.dart';
import 'package:flutter_getx/ui/user/model/login_info.dart';

class CacheUtils{

  static LoginInfo? _loginInfo;

  static saveLoginInfo(LoginInfo loginInfo){
    _loginInfo = loginInfo;
    SpUtil.putObject(Constant.loginInfo, loginInfo);
    loginInfo.postEvent();
  }

  static LoginInfo? getLoginInfo(){
    if(null != _loginInfo){
      return _loginInfo;
    }
    dynamic json = SpUtil.getObject(Constant.loginInfo);
    if(null != json) {
      _loginInfo = LoginInfo.fromJson(json);
    }
    return _loginInfo;
  }

  static bool isLogin() => getLoginInfo() != null;

  static String? authorization(){
    if(isLogin()){
      return "${getLoginInfo()!.tokenType} ${getLoginInfo()!.accessToken}";
    }
    return null;
  }


  /*static UserInfo? _userInfo;

  static saveUserInfo(UserInfo userInfo){
    _userInfo = userInfo;
    SpUtil.putObject(Constant.userInfo, userInfo);
    userInfo.postEvent();
  }

  static UserInfo? getUserInfo(){
    if(null == _userInfo){
      dynamic json = SpUtil.getObject(Constant.userInfo);
      if(null != json) {
        _userInfo = UserInfo.fromJson(json);
      }
    }
    return _userInfo;
  }*/


  static logout(){
    _loginInfo = null;
    //_userInfo = null;

    SpUtil.remove(Constant.loginInfo);
    SpUtil.remove(Constant.userInfo);
  }

}